var o = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) {
    return typeof e;
} : function (e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
};
window.tywx || (window.tywx = {}), tywx.PropagateInterface = {
    ShareConfig: {},
    getShareConfigInfo: function (e) {
        "object" != (void 0 === e ? "undefined" : o(e)) && (e = {});
        var t = new Date().getTime();
        e.act = "api.getShareConfig", e.time = t, e.game_mark = tywx.SystemInfo.cloudId + "-" + tywx.SystemInfo.gameId;
        var n = this.getConfigSignStr(e), i = [];
        for (var a in e) i.push(a + "=" + e[a]);
        i.push("sign=" + n);
        var r = tywx.SystemInfo.shareManagerUrl + "?" + i.join("&");
        tywx.HttpUtil.httpGet({
            url: r
        }, function (e) {
            for (var t in tywx.LOGE("GET_SHARE_CONFIG_INFO_SUCCESS", JSON.stringify(e)), e.retmsg) tywx.PropagateInterface.ShareConfig[t] = e.retmsg[t];
            tywx.NotificationCenter.trigger(tywx.EventType.GET_SHARE_CONFIG_SUCCESS, e);
        }, function (e) {
            tywx.LOGE("GET_SHARE_CONFIG_INFO_FAIL", JSON.stringify(e)), tywx.NotificationCenter.trigger(tywx.EventType.GET_SHARE_CONFIG_FAIL, e);
        });
    },
    _cachedShareConfig: void 0,
    _pendingFlag: !1,
    _rawConfigInfo: void 0,
    getShareConfigInfoAutoWeight: function () {
        void 0 == this._cachedShareConfig ? this._pendingFlag = !0 : tywx.NotificationCenter.trigger(tywx.EventType.GET_SHARE_CONFIG_SUCCESS, this._shuffleByWeights());
    },
    _doHttpGetShareConfig: function () {
        var e = {}, t = new Date().getTime();
        e.act = "api.getShareConfig", e.time = t, e.game_mark = tywx.SystemInfo.cloudId + "-" + tywx.SystemInfo.gameId;
        var n = this.getConfigSignStr(e), o = [];
        for (var i in e) o.push(i + "=" + e[i]);
        o.push("sign=" + n);
        var a = tywx.SystemInfo.shareManagerUrl + "?" + o.join("&"), r = this;
        tywx.HttpUtil.httpGet({
            url: a
        }, function (e) {
            r._cachedShareConfig = e.retmsg, r._pendingFlag && (tywx.NotificationCenter.trigger(tywx.EventType.GET_SHARE_CONFIG_SUCCESS, r._shuffleByWeights()),
                r._pendingFlag = !1);
        }, function (e) {
            r._pendingFlag && (tywx.NotificationCenter.trigger(tywx.EventType.GET_SHARE_CONFIG_FAIL, e),
                r._pendingFlag = !1);
            var t = function () {
                r._doHttpGetShareConfig();
            };
            "undefined" != typeof cc && cc.director ? tywx.Timer.setTimer(cc.director, t, 10, 0, 0) : setTimeout(t, 1e4);
        });
    },
    _shuffleByWeights: function () {
        var e = {};
        for (var t in this._cachedShareConfig) if ("shareExt" != t) {
            var n = this._cachedShareConfig[t];
            if (0 == n.length) e[t] = {}; else if (1 == n.length) e[t] = n[0]; else for (var o = n.reduce(function (e, t) {
                return e + t.weight;
            }, 0), i = Math.random() * o, a = 0; a < n.length; a++) if ((i -= n[a].weight) <= 0) {
                e[t] = n[a];
                break;
            }
        }
        return e;
    },
    getUserFeatureInfo: function () {
        var e = {}, t = new Date().getTime();
        e.act = "api.getUserFeature", e.cloud_id = tywx.SystemInfo.cloudId, e.game_id = tywx.SystemInfo.gameId,
            e.user_id = tywx.UserInfo.userId, e.time = t;
        var n = this.getConfigSignStr(e), o = [];
        for (var i in e) o.push(i + "=" + e[i]);
        o.push("sign=" + n);
        var a = tywx.SystemInfo.shareManagerUrl + "?" + o.join("&");
        tywx.HttpUtil.httpGet({
            url: a
        }, function (e) {
            tywx.NotificationCenter.trigger(tywx.EventType.GET_USER_FEATURE_SUCCESS, e);
        }, function (e) {
            tywx.NotificationCenter.trigger(tywx.EventType.GET_USER_FEATURE_FAIL, e);
        });
    },
    processRawShareConfigInfo: function () {
        this._cachedShareConfig = {};
        var e = tywx.TuyooSDK.ipLocInfo.loc ? tywx.TuyooSDK.ipLocInfo.loc[1] : "";
        if (e && this._rawConfigInfo) for (var t = Object.keys(this._rawConfigInfo), n = 0, o = t.length; n < o; n++) {
            var i = this._rawConfigInfo[t[n]];
            if (0 < i.length) {
                this._cachedShareConfig[t[n]] = [];
                for (var a = 0, r = i.length; a < r; a++) {
                    var s = i[a], l = s.area, c = !1;
                    if (l && l instanceof Array) if (0 == l.length) c = !1; else for (var d in l) {
                        if (l[d].indexOf(e) > -1) {
                            c = !0;
                            break;
                        }
                    }
                    c || this._cachedShareConfig[t[n]].push(s);
                }
            }
        }
    },
    getConfigSignStr: function (e) {
        for (var t = Object.keys(e).sort(), n = "", o = 0; o < t.length; o++) {
            var i = t[o];
            "act" != i && "sign" != i && (n += i + "=" + e[i]);
        }
        return tywx.hex_md5("market.tuyoo.com-api-" + n + "-market.tuyoo-api") || "";
    },
    getWeightShareConfigInfoByTypeWithShare: function (e, t, n, i, a) {
        "object" != (void 0 === e ? "undefined" : o(e)) && (e = {});
        var r = this, s = new Date().getTime();
        e.act = "api.getShareConfig", e.time = s, e.game_mark = tywx.SystemInfo.cloudId + "-" + tywx.SystemInfo.gameId;
        var l = this.getConfigSignStr(e), c = [];
        for (var d in e) c.push(d + "=" + e[d]);
        c.push("sign=" + l);
        var u = tywx.SystemInfo.shareManagerUrl + "?" + c.join("&");
        n = void 0 == n ? function () {
            console.log("分享成功");
        } : n;
        tywx.HttpUtil.httpGet({
            url: u
        }, function (o) {
            for (var s in o.retmsg) tywx.PropagateInterface.ShareConfig[s] = o.retmsg[s];
            var l = r._myShuffleByWeights(o.retmsg);
            if (console.log("zzw log2:", l), e.share_type && l && l[e.share_type] && n && "function" == typeof n) {
                var c = l[e.share_type];
                c.shareContent = r._replaceKeyContent(c.shareContent), a ? tywx.ShareInterface.shareWithCaptureInfo(c.shareContent, c.sharePicUrl, c.sharePointId, c.shareSchemeId, n, i, t, a) : tywx.ShareInterface.share(c.shareContent, c.sharePicUrl, c.sharePointId, c.shareSchemeId, n, i, t);
            }
        }, function (e) {
            i && "function" == typeof i && i(e);
        });
    },
    _replaceKeyContent: function (e) {
        var t = "";
        return "" != (t = Global.tableModel.getCreateTableNo() + "") && (e = e.replace("@key1@", t)),
        "" != (t = Global.utils.getFixString(Global.userProfile.getUserName(), 5)) && (e = e.replace("@key2@", t)),
            e;
    },
    _myShuffleByWeights: function (e) {
        if (e) {
            var t = {};
            for (var n in e) if ("shareExt" != n) {
                var o = e[n];
                if (0 == o.length) t[n] = {}; else if (1 == o.length) t[n] = o[0]; else for (var i = o.reduce(function (e, t) {
                    return e + t.weight;
                }, 0), a = Math.random() * i, r = 0; r < o.length; r++) if ((a -= o[r].weight) <= 0) {
                    t[n] = o[r];
                    break;
                }
            }
            return t;
        }
    }
}
